<template>
  <el-card>
    <template #header>
      <div class="flex">
        <div class="flex1">
          {{ isNew ? '新增勋章' : '修改勋章' }}
        </div>
      </div>
    </template>

    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      class="page"
    >
      <el-form-item label="勋章名称" prop="name">
        <el-input
          v-model="formData.name"
          placeholder="输入勋章名称，限制6字以内"
        />
      </el-form-item>
      <el-form-item label="勋章图标" prop="iconUrl">
        <ImageUpload v-model="formData.iconUrl"></ImageUpload>
      </el-form-item>
      <el-form-item label="勋章图标2" prop="iconUrlC">
        <ImageUpload
          v-model="formData.iconUrlC"
          height="46"
          width="210"
        ></ImageUpload>
      </el-form-item>
      <el-form-item label="对应积分" prop="integral">
        >=
        <el-input-number
          style="margin: 0 10px"
          v-model="formData.integral"
          maxlength="10"
          placeholder="1 ~ 1000000000"
          controls-position="right"
          :min="1"
          :max="1000000000"
          :precision="0"
        />
        分
      </el-form-item>
    </el-form>

    <div>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">
        提交
      </el-button>
      <el-button @click="$router.back()">取消 </el-button>
    </div>
  </el-card>
</template>
<script lang="ts" setup>
import API from '@/api/api';
import { useMessage } from '@/hooks/useMessage';
import { createRequireRole } from '@/utils/rules';
import { useRequest } from 'vue-hooks-plus';
const route = useRoute();
const router = useRouter();
const message = useMessage(); // 消息弹窗
const isNew = !route.query.id;
useRequest(
  async () => {
    return API.medalController.info({
      id: route.query.id,
    });
  },
  {
    ready: !!route.query.id,
    onSuccess(res: any) {
      formData.value = res;
    },
  }
);

const submitReq = useRequest(
  async (sendData: any) => {
    if (isNew) {
      return API.medalController.add(sendData);
    }
    return API.medalController.update({
      id: route.query.id,
      ...sendData,
    });
  },
  {
    manual: true,
  }
);

const formLoading = ref(false); // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref(''); // 表单的类型：create - 新增；update - 修改
const formData = ref({});
const formRules = reactive({
  name: [createRequireRole()],
  integral: [createRequireRole()],
  iconUrl: [createRequireRole()],
  iconUrlC: [createRequireRole()],
});
const formRef = ref(); // 表单 Ref

const submitForm = async () => {
  // 校验表单
  if (!formRef) return;
  const valid = await formRef.value.validate();
  if (!valid) return;
  // 提交请求
  formLoading.value = true;

  const data = formData.value;
  await submitReq.runAsync(data);
  if (isNew) {
    message.success('新增成功');
  } else {
    message.success('更新成功');
  }
  router.back();
};

/** 重置表单 */
const resetForm = () => {
  formData.value = {};
  formRef.value?.resetFields();
};
</script>
